<?php if (!defined('BASEPATH'))    exit('No direct script access allowed');

class Post extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('m_post');
    }
    
    function insert_process() {
        $new_name = date('Ymd-his');
        $this->load->library('upload');
        $config['upload_path'] = './Images/Post_Thumb/'; 
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '800';
        $config['file_name'] = "poststhumb-$new_name";
        
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('thumb')) {
            $upld = array('message' => $this->upload->display_errors());
        } else {
            $upld = array('message' => $this->upload->data());
        }
        
        $data['title'] = $this->input->post('post_title');
        $data['content'] = $this->input->post('post_content');
        $data['cate'] = intval($this->input->post('category'));
        $data['loc'] = intval($this->input->post('location'));
        $data['latitude'] = $this->input->post('latitude');
        $data['longitude'] = $this->input->post('longitude');
        $data['thumb'] = $upld['message']['file_name'];
        
        $cek = $this->m_post->insert_post($data);
        if($cek){
            redirect('User/new_entry');
        }else{
            echo "<script>alert('Error | inserting data failed')</script>";
        }
        
    }
    
    function update_post($param) {
        if($param !=""){
            $data['title'] = "Edit Entry";
            $data['tab_title'] = "Edit Entry";
            $data['icon'] = "file-text";
            $data['page'] = "edit_entry";

            $this->load->model('m_post');
            $data['post'] = $this->m_post->select_post_byId($param);

            if($this->input->post('btn_update_post') != ""){
                $this->load->library('form_validation');

                $this->form_validation->set_rules('post_title', 'Post Title', 'required');
                $this->form_validation->set_rules('post_content', 'Post Content', 'required');

                if ($this->form_validation->run() != FALSE){
                    $post_data['title'] = $this->input->post('post_title');
                    $post_data['content'] = $this->input->post('post_content');

                    if($this->input->post('category') != ""):
                        $post_data['category'] = $this->input->post('category');
                    else:
                        $post_data['category'] = $data['post']['id_category'];
                    endif;


                    if($this->input->post('location') != ""):
                        $post_data['location'] = $this->input->post('location');
                    else:
                        $post_data['location'] = $data['post']['id_location'];
                    endif;

                    if($this->input->post('latitude') != ""):
                        $post_data['latitude'] = $this->input->post('latitude');
                    else:
                        $post_data['latitude'] = $data['post']['latitude'];
                    endif;

                    if($this->input->post('longitude') != ""):
                        $post_data['longitude'] = $this->input->post('longitude');
                    else:
                        $post_data['longitude'] = $data['post']['longitude'];
                    endif;
                    
                    $new_name = date('Ymd-his');

                    $this->load->library('upload');
                    $config['upload_path'] = './Images/Post_Thumb/'; 
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '800';
                    $config['file_name'] = "poststhumb-$new_name";

                    $this->upload->initialize($config);
                    if (!$this->upload->do_upload('thumb')) {
                        $upld = array('message' => $this->upload->display_errors());
                    } else {
                        $upld = array('message' => $this->upload->data());
                    }
                    if(isset($upld['message']['file_name'])){
                        $post_data['post_thumb'] = $upld['message']['file_name'];
                    }
                    $post_data['id'] = $data['post']['id_post'];

                    $this->load->model('m_post');
                    $cek = $this->m_post->update_post($post_data);

                    if($cek ==  TRUE):
                        $data['message'] = "1";
                    else:
                        $data['message'] = "0";
                    $data['error'] = "Update Profile Failed";
                    endif;

                }else{
                    $data['message'] = "0";
                    $data['error'] = validation_errors();
                }
            }
            $this->load->model('m_category');
            $data['categories'] = $this->m_category->get_all_category();
            $this->load->model('m_location');
            $data['locations'] = $this->m_location->get_all_location();

            $this->load->view('user/home',$data);
        }else{
            redirect('User/all_entries');
        }
    }

    function update_post_byAdmin($param) {
        if($param !=""){
            $data['title'] = "Edit Entry";
            $data['tab_title'] = "Edit Entry";
            $data['icon'] = "file-text";
            $data['page'] = "edit_entry";

            $this->load->model('m_post');
            $data['post'] = $this->m_post->select_post_byId($param);

            if($this->input->post('btn_update_post') != ""){
                $this->load->library('form_validation');

                $this->form_validation->set_rules('post_title', 'Post Title', 'required');
                $this->form_validation->set_rules('post_content', 'Post Content', 'required');

                if ($this->form_validation->run() != FALSE){
                    $post_data['title'] = $this->input->post('post_title');
                    $post_data['content'] = $this->input->post('post_content');

                    if($this->input->post('category') != ""):
                        $post_data['category'] = $this->input->post('category');
                    else:
                        $post_data['category'] = $data['post']['id_category'];
                    endif;


                    if($this->input->post('location') != ""):
                        $post_data['location'] = $this->input->post('location');
                    else:
                        $post_data['location'] = $data['post']['id_location'];
                    endif;

                    if($this->input->post('latitude') != ""):
                        $post_data['latitude'] = $this->input->post('latitude');
                    else:
                        $post_data['latitude'] = $data['post']['latitude'];
                    endif;

                    if($this->input->post('longitude') != ""):
                        $post_data['longitude'] = $this->input->post('longitude');
                    else:
                        $post_data['longitude'] = $data['post']['longitude'];
                    endif;
                    
                    $new_name = date('Ymd-his');

                    $this->load->library('upload');
                    $config['upload_path'] = './Images/Post_Thumb/'; 
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '800';
                    $config['file_name'] = "poststhumb-$new_name";

                    $this->upload->initialize($config);
                    if (!$this->upload->do_upload('thumb')) {
                        $upld = array('message' => $this->upload->display_errors());
                    } else {
                        $upld = array('message' => $this->upload->data());
                    }
                    if(isset($upld['message']['file_name'])){
                        $post_data['post_thumb'] = $upld['message']['file_name'];
                    }
                    $post_data['id'] = $data['post']['id_post'];

                    $this->load->model('m_post');
                    $cek = $this->m_post->update_post($post_data);

                    if($cek ==  TRUE):
                        $this->session->set_flashdata('message_type', '1');
                        $this->session->set_flashdata('message', 'Updating Data Success');
                    else:
                        $this->session->set_flashdata('message_type', '0');
                        $this->session->set_flashdata('message', 'Updating Data Failed');
                    endif;

                }else{
                    $this->session->set_flashdata('message_type', '0');
                    $this->session->set_flashdata('message', validation_errors());
                }
            }
            $this->load->model('m_category');
            $data['categories'] = $this->m_category->get_all_category();
            $this->load->model('m_location');
            $data['locations'] = $this->m_location->get_all_location();

            redirect("Admin/edit_post/$param");
        }else{
            redirect('User/all_entries');
        }
    }

    function delete_post($param){
        if($param != ""){
            $this->load->model('m_post');
            if($this->input->post('btn_delete_post') != ""){
                $cek = $this->m_post->delete_post($param);
                if($cek){
                    $data['message'] = 1;
                    $data['visible'] = "style='display:none'";
                }else{
                    $data['message'] = 0;
                    $data['visible'] = "";
                }
            }

            $data['title'] = "Delete Entry";
            $data['tab_title'] = "Delete Entry";
            $data['icon'] = "trash";
            $data['page'] = "delete_entry";

            $data['post_data'] = $this->m_post->select_post_byId($param);

            $this->load->view('user/home',$data);
        }else{
            redirect ('User/all_entries');
        }
    }

    function delete_post_byAdmin($param){
        if($this->input->post("btn_del_post_byadmn") != ""){
            $cek = $this->m_post->delete_post($param);
            if($cek){
                $this->session->set_flashdata('message_type', '1');
                $this->session->set_flashdata('message', 'Deleting Data Success');
            }else{
                $this->session->set_flashdata('message_type', '0');
                $this->session->set_flashdata('message', 'Deleting Data Failed');
            }
            redirect("Admin/post_page");
        }else{
            redirect("Admin/post_page");
        }
    }
    
}
